/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.pages.game.editor;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.ResourceReference;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.collections.MiniMap;
import org.apache.wicket.util.template.TextTemplateHeaderContributor;

import es.sonxurxo.androidrunner.view.web.components.map.MapPanel;
import es.sonxurxo.androidrunner.view.web.pages.BasePage;

/**
 * @author "Xurxo Mendez Perez"
 * 
 */
@AuthorizeInstantiation( { "ADMIN", "PLAYER" })
public class GameEditorPage extends BasePage {

	private static final String JQUERY_URL = "js/jquery-1.3.2.min.js";
	private static final String GAME_OBJECTS_URL = "js/gameObjects.js";
	private static final String MAP_MANAGER_URL = "js/mapManager.js";

	public GameEditorPage() {
		super();
		add(HeaderContributor.forJavaScript(JQUERY_URL));
		add(HeaderContributor.forJavaScript(new ResourceReference(
				GameEditorPage.class, MAP_MANAGER_URL)));
		add(HeaderContributor.forJavaScript(GAME_OBJECTS_URL));
		add(TextTemplateHeaderContributor.forJavaScript(
				GameEditorPage.class, MAP_MANAGER_URL, new Model(
						getVariablesMap())));
		add(new MapPanel("mapPanel"));
	}

	@Override
	protected String getTitle() {
		return getLocalizer()
				.getString("gameEditor.title", GameEditorPage.this);
	}

	private MiniMap getVariablesMap() {
		MiniMap variables = new MiniMap(5);
		// TODO : remove hard dependency on servlet-api < 2.5
		HttpServletRequest request = getWebRequestCycle().getWebRequest()
				.getHttpServletRequest();
		String contextPath = request.getContextPath();

		String createURL = request.getScheme() + "://" + request.getServerName()
				+ ":" + String.valueOf(request.getServerPort()) + contextPath
				+ "/create";

		String viewURL = request.getScheme() + "://" + request.getServerName()
			+ ":" + String.valueOf(request.getServerPort()) + contextPath
			+ "/gameDetails/gameCreated/true/gameId/";

		variables.put("COIN_IMG", contextPath + "/images/coin.png");
		variables.put("QUESTION_IMG", contextPath + "/images/question.png");
		variables.put("BONUS_IMG", contextPath + "/images/bonus.png");
		variables.put("CREATE_URL", createURL);
		variables.put("VIEW_URL", viewURL);

		return variables;
	}
}
